package tt.dz.biz.dao.paymentgateway;

import java.util.List;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.vo.PayChannelVO;

@Repository("payChannelDAO")
public class PayChannelDAO
  extends BaseDAO
{
  private static final String LIST_ALL_PAYCHANNEL = "select  a.channel_code,a.channel_name,a.callback_url,a.plat_account,a.is_realtime,a.public_key,a.sign_type,a.remark,a.is_valid  from ms_pay_channel as a";
  private static final String INSERT_PAYCHANNEL = "insert into ms_pay_channel(channel_code,channel_name,callback_url,plat_account,is_realtime,public_key,sign_type,remark,is_valid)  values(:channelCode,:channelName,:callbackUrl,:platAccount,:isRealtime,:publicKey,:signType,:remark,:isValid )";
  private static final String UPDATE_PAYCHANNEL = "update  ms_pay_channel set channel_name = :channelName,callback_url = :callbackUrl,plat_account = :platAccount,is_realtime = :isRealtime,public_key = :publicKey,sign_type = :signType,remark = :remark,is_valid = :isValid where channel_code = :channelCode";
  
  public List<PayChannelVO> listAllPayChannel()
  {
    return queryForList("select  a.channel_code,a.channel_name,a.callback_url,a.plat_account,a.is_realtime,a.public_key,a.sign_type,a.remark,a.is_valid  from ms_pay_channel as a", null, PayChannelVO.class);
  }
  
  public int insertPayChannel(PayChannelVO payChannelVO)
  {
    return save("insert into ms_pay_channel(channel_code,channel_name,callback_url,plat_account,is_realtime,public_key,sign_type,remark,is_valid)  values(:channelCode,:channelName,:callbackUrl,:platAccount,:isRealtime,:publicKey,:signType,:remark,:isValid )", payChannelVO);
  }
  
  public int updatePayChannel(PayChannelVO payChannelVO)
  {
    return save("update  ms_pay_channel set channel_name = :channelName,callback_url = :callbackUrl,plat_account = :platAccount,is_realtime = :isRealtime,public_key = :publicKey,sign_type = :signType,remark = :remark,is_valid = :isValid where channel_code = :channelCode", payChannelVO);
  }
  
  public PayChannelVO queryChannelByCode(String channelCode)
  {
    try
    {
      StringBuilder sql = new StringBuilder("select  a.channel_code,a.channel_name,a.callback_url,a.plat_account,a.is_realtime,a.public_key,a.sign_type,a.remark,a.is_valid  from ms_pay_channel as a");
      sql.append(" where a.channel_code=:channelCode and a.is_valid=1");
      PayChannelVO channel = new PayChannelVO();
      channel.setChannelCode(channelCode);
      return (PayChannelVO)queryForObject(sql.toString(), channel, PayChannelVO.class);
    }
    catch (EmptyResultDataAccessException e) {}
    return null;
  }
  
  public List<PayChannelVO> queryChannelListByName(String name)
  {
    StringBuilder sql = new StringBuilder("select  a.channel_code,a.channel_name,a.callback_url,a.plat_account,a.is_realtime,a.public_key,a.sign_type,a.remark,a.is_valid  from ms_pay_channel as a");
    PayChannelVO param = new PayChannelVO();
    param.setChannelName(name);
    sql.append(" where channel_name=:channelName");
    return queryForList(sql.toString(), param, PayChannelVO.class);
  }
}
